import { configure, getLogger } from 'log4js';

configure({
    appenders: {
        cheese: {
            type: 'dateFile',
            filename: 'logs/cheese',
            pattern: '-yyyy-MM-dd.log',
            alwaysIncludePattern: true,
            category: 'normal',
        },
    },
    categories: { default: { appenders: ['cheese'], level: 'info' } },
});

const Logger = getLogger('cheese');

export function logger(req, res, next) {
    const statusCode = res.statusCode;
    const logFormat = `${req.method} ${req.originalUrl} ip: ${req.ip} statusCode: ${statusCode}`;
    next();

    if (statusCode >= 500) {
        Logger.error(logFormat);
    } else if (statusCode >= 400) {
        Logger.warn(logFormat);
    } else {
        Logger.info(logFormat);
    }
}